//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#pragma once

#define __MAX__PATH 260


#define XY_NOERR  0x00000000
#define XY_E_FAIL 0xC000A001

//define protocol type
#define  XMDM     0 //xmodem
#define  XMDMCRC  1 //xmodem crc
#define  WXMDM    2 //wxmodem
#define  YMDM     3 //ymodem
#define  YMDMG    4 //ymodem G


/*define Xmodem state*/
#define  X_START    0   /* XMIT : waiting to begin */
                        /* RECV : waiting to begin */
#define  X_INIT	    1   /* RECV : sent initial NAK, 'C' or 'W' */
#define  X_WAIT	    2   /* XMIT : sent a packet, waiting for ACK */
                        /* RECV : wait for start of next packet */
#define  X_FILE	    3   /* XMIT : waiting for initial protocol character */
#define  X_FILEWAIT	4   /* XMIT : sent file header, waiting for ACK */
#define  X_PACKET	5   /* RECV : receiving a packet */
#define  X_EOT		6   /* XMIT : sent an EOT, waiting for ACK */
#define  X_ENDWAIT	7   /* XMIT : sent null filename, waiting for ACK */



/* status code definitions */
#define XmFullBlk        1 /* a full block complete */
#define XmErrNone        2 /* normal status, nothing occured */
#define XmValidBlk       3 /* a valid block */
#define XmNewFile        4 /* a file info header packet */
#define	XmDone		    -1	/* done */
#define	XmErrInt	    -2	/* internal error */
#define	XmErrSys	    -3	/* system error, see errno */
#define	XmErrNotOpen	-4	/* communication channel not open */
#define	XmErrCantOpen	-5	/* can't open file, see errno */
#define	XmErrInitTo	    -10	/* transmitter failed to respond to init req. */
#define	XmErrSequence	-11	/* packet received out of sequence */
#define	XmErrCancel	    -12	/* cancelled by remote end */
#define	XmErrRcvTo	    -13	/* remote end timed out during transfer */
#define XmErrXmitTo     -14 /* can't send char to remote end */
#define XmErrIgnore     -15 /* char ignore */
#define XmErrYmGRxTo    -16 /* Ymodem G receive time out*/


/* PACKET DONE ACTION STATUS */
#define AC_NONE         0xA000
#define AC_REJECT       0xA001
#define AC_ACCEPT       0xA002
#define AC_CANCEL       0xA003
#define AC_YMD1STPK     0xA004
#define AC_EOT          0xA005
#define AC_FILEINFO     0xA006
#define AC_TIMEOUT      0xA007
#define AC_DATA         0xA008

/* INTERNAL */
#define	SOH	    1	/* ^A */
#define	STX	    2	/* ^B */
#define	EOT	    4	/* ^D */
#define	ACK	    6	/* ^F */
#define	DLE	    16	/* ^P */
#define	XON	    17	/* ^Q */
#define	XOFF    19	/* ^S */
#define	NAK	    21	/* ^U */
#define	SYN	    22	/* ^V */
#define	CAN	    24	/* ^X */

/* Packet Status */
#define PK_NONE        0xFF /* default packet status, none packet */
#define PK_FILEINFO    0x0A /* file information packet*/
#define PK_DUPLICATE   0x0B /* duplicate packet, already accepted */
#define PK_VALIDDATA   0x0C /* valid data packet */
#define PK_REJECT      0x0D /* invalid packet, rejected */
#define PK_EOT         0x0E /* end of transfer packet */
#define PK_CANCEL      0x0F /* the transfer has been canceled */
#define PK_REXMIT      0x10 /* remote send nak, resend packet */
#define PK_TIMEOUT     0x11 /* send packet time out */

#define	MAXERROR	10
//#define SEC 0
//#if SEC
//#define	INITTO		10	/* initialization timeout, basic xmodem */
//                        /* Xmodem, Ymodem, YmodemG*/
//#define	INITTO2		3	/* initialization timeout */
//                        /* WXmodem, XmodemCRC*/
//#define	PKTTO		5	/* in-packet receive timeout */
//#else
#define	INITTO		10000	/* initialization timeout, basic xmodem */
                        /* Xmodem, Ymodem, YmodemG*/
#define	INITTO2		3000	/* initialization timeout */
                        /* WXmodem, XmodemCRC*/
#define	PKTTO		5000	/* in-packet receive timeout */
//#endif

#define	MAXPACKET	1024/* max packet length */
#define MINPACKET   128 /* mini packet length */
#define PKGHDR      3   /* Packet Head Reserved */
#define CRCLEN      2   /* Two Byte CRC Reserved */
#define CSMLEN      1   /* One Byte Checksum Reserved */
#define BLOCKLEN    (MAXPACKET + PKGHDR + CRCLEN)
/*Xmodem & 1K Xmodem Packet Format
-------------------------------------------------------------------
| SOH/STX 1b | BLK NO 1b | ~BLK NO 1b | DTAT 128b/1024b |CRC16 2b |
-------------------------------------------------------------------
*/
typedef struct _xy_pkg {
    int  /* RECV OUT/ XMIT IN  */PacketStatus;/* 1st packet, mid packet, end packet, faild packet*/
    int  /* RECV OUT/ XMIT IN  */PacketDataLen;/* The Length of Data In Packet*/
    int  /* RECV IN / XMIT OUT */ ActionStatus;/* reject .accpet .cancel . none*/
    char PacketData[BLOCKLEN]; /* 1029 */
    char Reserved[3];/* Make sure the Structrue aligned */
}XYM_PKG, *P_XYM_PKG;

typedef struct _blk_str {
    char Hdblk[3];//block header
    char Data[BLOCKLEN]; //block data start
}BLK_STR, *P_BLK_STR;



unsigned int Read( /* [out] */ unsigned char *ebbData,
            /* [in]  */ unsigned int cbData,
            /* [out] */ unsigned int *pdwRead);
unsigned int Write(
    /* [out] */ unsigned char* ebbData,
    /* [in]  */ unsigned int cbData,
    /* [out] */ unsigned int *pdwWrt);
unsigned int Control(
    /* [in] */ int nControlCode,
    /* [in] */ unsigned char *ebbInData,
    /* [out] */unsigned char *ebbOutData);
//RECV Function
    int RECV(char* PacketBuf, int acstatus);
    int RECV_INIT(int);
    int RECV_START(int protocol);
    int RECV_RECV(char* PacketBuf);
    int RECV_PACKET(char* Packet, int* pkstatus);
    int RECV_ACCEPT();
    int RECV_REJECT();

//XMIT Function
    int XMIT(char *packet, int pkstatus, int xmitl);
    int XMIT_INIT(int protocol);
    int XMIT_BUILD(char *packet, int xmitl);
    int XMIT_LISTEN();
    int XMIT_XMIT(char* packet);


void XYmodemInit();
//XYMDM Utils
int CalCrc(char *buf, int len);
int CalcChecksum(char *ptr, int count);
int RecvStr(char *str, int len, int to);
int SendStr(char *str, int len);
int SendChar(char c);
int Flush(char c);
int Cancel();



